#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=adguardhome_lede
PKG_VERSION:=v0.108.0-b.46
PKG_RELEASE:=1

##PKG_SOURCE_NAME:=AdGuardHome-master
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/AdguardTeam/AdGuardHome.git
#PKG_SOURCE_DATE:=2025-02-21
#PKG_SOURCE_VERSION=master
PKG_SOURCE_VERSION:=782a1a982057b1431d7f4ac2f51955dade5b870f
PKG_MIRROR_HASH:=skip
FIXUP:=1

##PKG_SOURCE_SUBDIR:=$(PKG_SOURCE_NAME)
##PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SOURCE_NAME)

include $(INCLUDE_DIR)/package.mk

PKG_LICENSE:=GPL-3.0-only
PKG_LICENSE_FILES:=LICENSE.txt
PKG_MAINTAINER:=Dobroslaw Kijowski <dobo90@gmail.com>

PKG_BUILD_DEPENDS:=golang/host node/host node-yarn/host
PKG_BUILD_PARALLEL:=1
PKG_USE_MIPS16:=0

PKG_CONFIG_DEPENDS:= \
	CONFIG_ADGUARDHOME_LEDE_COMPRESS_GOPROXY \
	CONFIG_ADGUARDHOME_LEDE_COMPRESS_UPX
GO_PKG:=github.com/AdguardTeam/AdGuardHome
GO_PKG_BUILD_PKG:=github.com/AdguardTeam/AdGuardHome

AGH_BUILD_TIME:=$(shell date -d @$(SOURCE_DATE_EPOCH) +%FT%TZ%z)
AGH_VERSION_PKG:=github.com/AdguardTeam/AdGuardHome/internal/version
GO_PKG_LDFLAGS_X:=$(AGH_VERSION_PKG).channel=release \
	$(AGH_VERSION_PKG).version=$(PKG_VERSION) \
	$(AGH_VERSION_PKG).buildtime=$(AGH_BUILD_TIME) \
	$(AGH_VERSION_PKG).goarm=$(GO_ARM) \
	$(AGH_VERSION_PKG).gomips=$(GO_MIPS)

include $(INCLUDE_DIR)/package.mk
include $(TOPDIR)/feeds/packages/lang/golang/golang-package.mk

define Package/adguardhome_lede
	SECTION:=net
	CATEGORY:=Network
	TITLE:=(LEDE) Network-wide ads and trackers blocking DNS server
	URL:=https://github.com/AdguardTeam/AdGuardHome
	DEPENDS:=$(GO_ARCH_DEPENDS) +ca-bundle
endef

define Package/adguardhome_lede/conffiles
#/etc/AdGuardHome.yaml
/etc/config/AdGuardHome
endef

define Package/adguardhome_lede/description
(LEDE)Free and open source, powerful network-wide ads and trackers blocking DNS server.
endef
define Package/adguardhome_lede/config
config ADGUARDHOME_LEDE_COMPRESS_GOPROXY
	bool "Compiling with GOPROXY proxy"
	default y

config ADGUARDHOME_LEDE_COMPRESS_UPX
	bool "Compress executable files with UPX"
	default n
endef

ifeq ($(CONFIG_ADGUARDHOME_LEDE_COMPRESS_GOPROXY),y)
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
endif

define Build/Compile
	( \
		pushd $(PKG_BUILD_DIR) ; \
		npm audit fix --force ; \
		NODE_OPTIONS=--openssl-legacy-provider make js-deps js-build ; \
		popd ; \
		$(call GoPackage/Build/Compile) ; \
	)
endef

define Package/adguardhome_lede/install
	$(call GoPackage/Package/Install/Bin,$(1))
	##$(INSTALL_DIR) $(1)/etc/init.d
	##$(INSTALL_BIN) ./files/adguardhome.init $(1)/etc/init.d/adguardhome

	##$(INSTALL_DIR) $(1)/etc/config
	##$(INSTALL_DATA) ./files/adguardhome.config $(1)/etc/config/adguardhome
endef

$(eval $(call GoBinPackage,adguardhome_lede))
$(eval $(call BuildPackage,adguardhome_lede))
